﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Xpus.Website.Utilities
{
	/// <summary>
	/// 表示MIME类型。
	/// </summary>
	public sealed class MimeType
	{
		/// <summary>
		/// 构造一个<see cref="MimeType"/>对象。
		/// </summary>
		/// <param name="type">类型名。</param>
		/// <param name="subtype">子类型名。</param>
		/// <param name="description">说明。</param>
		/// <param name="extensions">支持的扩展名。</param>
		private MimeType(string type, string subtype, string description, params string[] extensions)
		{
			this.Type = type;
			this.Subtype = subtype;
			this.Name = string.Format("{0}/{1}", type, subtype);
			this.Description = description;
			this.Extensions = extensions;
			this.Extension = extensions.Merge("; ");
		}

		/// <summary>
		/// 构造一个<see cref="MimeType"/>对象。
		/// </summary>
		/// <param name="type">类型名。</param>
		/// <param name="subtype">子类型名。</param>
		/// <param name="description">说明。</param>
		/// <param name="extension">支持的扩展名。多个扩展名之间用分号(;)隔开。</param>
		private MimeType(string type, string subtype, string description, string extension)
			: this(type, subtype, description, extension.Split(new char[] { ';' }))
		{
		}

		/// <summary>
		/// 名称，即Mime字符串。
		/// </summary>
		public string Name
		{
			get;
			private set;
		}

		/// <summary>
		/// 类型名。
		/// </summary>
		public string Type
		{
			get;
			private set;
		}

		/// <summary>
		/// 子类型名。
		/// </summary>
		public string Subtype
		{
			get;
			private set;
		}

		/// <summary>
		/// 说明。
		/// </summary>
		public string Description
		{
			get;
			private set;
		}

		/// <summary>
		/// 扩展名集合。
		/// </summary>
		public string[] Extensions
		{
			get;
			private set;
		}

		/// <summary>
		/// 用分号(;)隔开的所有扩展名。
		/// </summary>
		public string Extension
		{
			get;
			private set;
		}

		static Dictionary<string, MimeType> _allMimeTypes;

		/// <summary>
		/// 初始化<see cref="MimeType"/>类。
		/// </summary>
		static MimeType()
		{
			_allMimeTypes = new Dictionary<string, MimeType>();

			MimeType mt;

			mt = new MimeType("image", "bmp", "", ".bmp");
			_allMimeTypes.Add(mt.Name, mt);
			mt = new MimeType("image", "gif", "", ".gif");
			_allMimeTypes.Add(mt.Name, mt);
			mt = new MimeType("image", "ief", "", ".ief");
			_allMimeTypes.Add(mt.Name, mt);
			mt = new MimeType("image", "jpeg", "", ".jpg; .jpeg; .jpe");
			_allMimeTypes.Add(mt.Name, mt);
			mt = new MimeType("image", "png", "", ".png");
			_allMimeTypes.Add(mt.Name, mt);
			mt = new MimeType("image", "tiff", "", ".tiff; .tif");
			_allMimeTypes.Add(mt.Name, mt);
		}

		/// <summary>
		/// 判断一个Mime字符串是否为系统所支持的MIME类型。
		/// </summary>
		/// <param name="mime"></param>
		/// <returns></returns>
		public static bool IsValidMimeType(string mime)
		{
			return _allMimeTypes.ContainsKey(mime);
		}

		/// <summary>
		/// 根据名字获取MIME类型。
		/// </summary>
		/// <param name="name">名字。</param>
		/// <returns>对应的MIME类型。如果不存在，返回null。</returns>
		public static MimeType GetMimeType(string name)
		{
			if(IsValidMimeType(name))
				return _allMimeTypes[name];
			else
				return null;
		}

		/// <summary>
		/// 根据类型名字获取MIME类型。
		/// </summary>
		/// <param name="type">类型名字。</param>
		/// <returns>指定类型的所有MIME类型。</returns>
		public static MimeType[] GetMimeTypes(string type)
		{
			string t = type.ToLower();

			var q = from m in _allMimeTypes.Values
					where m.Type == t
					select m;

			return q.ToArray();
		}
	}
}
